package com.ewancle.utils;

import java.util.Random;
import java.net.NetworkInterface;
import java.net.InetAddress;
import java.util.Enumeration;

public class RandomMachineIdGenerator {
    private static Random random = new Random();

    // 使用 MAC 地址来生成一个随机数种子
    public static int getMachineIdFromMac() {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                byte[] mac = networkInterface.getHardwareAddress();
                if (mac != null) {
                    int seed = 0;
                    for (byte b : mac) {
                        seed += b;
                    }
                    return Math.abs(seed % 32);  // 保证机器ID在0到31之间
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return random.nextInt(32); // 默认随机返回
    }

    // 使用随机数生成数据中心ID
    public static int generateDataCenterId() {
        return random.nextInt(32);
    }

    public static void main(String[] args) {
        int machineId = getMachineIdFromMac();  // 基于MAC地址生成机器ID
        int dataCenterId = generateDataCenterId();  // 随机生成数据中心ID
        System.out.println("Generated Machine ID: " + machineId);
        System.out.println("Generated Data Center ID: " + dataCenterId);
    }
}

